package DAO;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

import com.sel.controle.Sessao;
import com.sel.modelo.Data;
import com.sel.modelo.Reserva;


public class ReservaDAO extends DAOGeneric<Reserva> 
{

    public static final String    TABELA = "reserva";
    public static final String[] COLUNAS = {"idreserva", "idEquipamento", "idCampi", "idTerceiro", "dataHoraInicalReserva", "dataHoraFinalReserva", "usuarioResponsavel", "idSala", "idLaboratorio"};

    public static final String SCRIPT_CRIACAO_TABELA_RESERVA = "CREATE TABLE "+ TABELA +"( " +
                                                                COLUNAS[0] +" INTEGER PRIMARY KEY," +
                                                                COLUNAS[1] +" INTEGER DEFAULT NULL," +
                                                                COLUNAS[2] +" INTEGER, " +
                                                                COLUNAS[3] +" INTEGER DEFAULT NULL, " +
                                                                COLUNAS[4] +" TEXT, " +
                                                                COLUNAS[5] +" TEXT, " +
                                                                COLUNAS[6] +" INTEGER, " +
                                                                COLUNAS[7] +" INTEGER DEFAULT NULL, " +
                                                                COLUNAS[8] +" INTEGER DEFAULT NULL" +
                                                             ");";
    
    public static final String SCRIPT_DELECAO_TABELA_RESERVA = "DROP TABLE IF EXISTS "+TABELA;
    
    public static ReservaDAO instance;
    
    public static ReservaDAO getInstance(Context context) 
    {
        if(instance == null)
        {
            instance = new ReservaDAO(context);
        }
        return instance;
    }
    
    public ReservaDAO(Context context) 
    {
        super(context);
    }

    @Override
    public String getNomeColunaPrimaryKey() 
    {
        return COLUNAS[0];
    }

    @Override
    public String getNomeTabela() 
    {
        return TABELA;
    }

    @Override
    public ContentValues entidadeParacontentValues(Reserva reserva) 
    {
        ContentValues values = new ContentValues();
        if (reserva.getId()> 0)
        {
            values.put(COLUNAS[0], reserva.getId());
        }
        values.put(COLUNAS[1], reserva.getIdEquipamento());
        values.put(COLUNAS[2], reserva.getIdCampi());
        values.put(COLUNAS[3], reserva.getIdTerceiro());
        values.put(COLUNAS[4], reserva.getDataHoraInicalReserva());
        values.put(COLUNAS[5], reserva.getDataHoraFinalReserva());
        values.put(COLUNAS[6], reserva.getUsuarioResponsavel());
        values.put(COLUNAS[7], reserva.getIdSala());
        values.put(COLUNAS[8], reserva.getIdLaboratorio());
        return values;
    }

    @Override
    public Reserva contentValuesParaEntidade(ContentValues contentValues) 
    {
        Reserva reserva = new Reserva();
        reserva.setId(contentValues.getAsInteger(COLUNAS[0]));
        reserva.setIdEquipamento(contentValues.getAsInteger(COLUNAS[1]));
        reserva.setIdCampi(contentValues.getAsInteger(COLUNAS[2]));
        reserva.setIdTerceiro(contentValues.getAsInteger(COLUNAS[3]));
        reserva.setDataHoraInicalReserva(contentValues.getAsString(COLUNAS[4]));
        reserva.setDataHoraFinalReserva(contentValues.getAsString(COLUNAS[5]));
        reserva.setUsuarioResponsavel(contentValues.getAsInteger(COLUNAS[6]));
        reserva.setIdSala(contentValues.getAsInteger(COLUNAS[7]));
        reserva.setIdLaboratorio(contentValues.getAsInteger(COLUNAS[8]));
        return reserva;
    }

    public List<Reserva> buscaReservasPorUsuario(int idUsuarioLogado, int tipo) 
    {
    	String where = "";
        if (tipo == 1)
        {
            where = "idEquipamento";
        }
        else if (tipo == 2)
        {
            where = "idLaboratorio";
        }
        else if (tipo == 3)
        {
            where = "idSala";
        }
        String query = "SELECT * " +
                         "FROM reserva " +
                        "WHERE usuarioResponsavel = " + idUsuarioLogado +" "+
                        " AND  "+where+" > 0 ";
        List<Reserva> reservas = this.recuperarPorQuery(query);
        return reservas;
    }

    public List<Reserva> validaIntervaloReserva(String dataHoraInicial, String validaHoraFinal, int idItemReserva ,int  tipo) 
    {
        String where = "";
        if (tipo == 1)
        {
            where = "idEquipamento";
        }
        else if (tipo == 2)
        {
            where = "idLaboratorio";
        }
        else if (tipo == 3)
        {
            where = "idSala";
        }
        String query = "SELECT * " +
                         "FROM reserva " +
                        "WHERE dataHoraInicalReserva " +
                      "BETWEEN '"+ dataHoraInicial +"' " +
                          "AND '"+ validaHoraFinal + "' " +
                          "AND "+where+" = " + idItemReserva;
        List<Reserva> result = this.recuperarPorQuery(query);
        return result;
    }

    public List<Reserva> validaReservaEquipamentoPorUsuario(String dataHoraInicial, String validaHoraFinal) 
    {
        String query = "SELECT * " +
		                "FROM reserva " +
		               "WHERE dataHoraInicalReserva " +
		             "BETWEEN '"+ dataHoraInicial +"' " +
		                 "AND '"+ validaHoraFinal + "' " +
		                " AND usuarioResponsavel = "+ Sessao.usuario.getId() +" "+
		                      "OR idTerceiro = "+Sessao.usuario.getId()+""+
		                " AND idEquipamento <> null";
        List<Reserva> result = this.recuperarPorQuery(query);
        return result;
    }
}
